package xyz.paragon.review.persistence.service.impl;

import org.springframework.stereotype.Service;
import xyz.paragon.review.persistence.domain.Review;
import xyz.paragon.review.persistence.repository.ReviewRepository;
import xyz.paragon.review.persistence.service.ReviewService;

import javax.persistence.EntityNotFoundException;

@Service
public class ReviewServiceImpl implements ReviewService {

    private final ReviewRepository reviewRepository;

    public ReviewServiceImpl(ReviewRepository reviewRepository) {
        this.reviewRepository = reviewRepository;
    }

    @Override
    public Iterable<Review> findAll() {
        return reviewRepository.findAll();
    }

    @Override
    public Review findById(Long id) {
        return reviewRepository.findById(id).orElseThrow(EntityNotFoundException::new);
    }

    @Override
    public Review save(Review newReview) {
        return reviewRepository.save(newReview);
    }

    @Override
    public void removeReview(Long id) {
        reviewRepository.deleteById(id);
    }

    @Override
    public void removeReviewsByProductId(Long id) {
        reviewRepository.removeAllByProductId(id);
    }

}
